package guda.house.web.action.fee;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.WaterRuleBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.form.rule.WaterRuleEditForm;
import guda.house.biz.form.rule.WaterRuleForm;
import guda.house.biz.query.RoleQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.query.WaterRuleQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.SysVarDO;
import guda.house.dal.dataobject.WaterRuleDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by well on 15/11/11.
 */
@Controller
public class WaterRuleAction {

    @Autowired
    private WaterRuleBiz waterRuleBiz;
    @Autowired
    private SysVarDAO sysVarDAO;

    @RequestMapping(value = "fee/water/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        WaterRuleQuery waterRuleQuery = new WaterRuleQuery();
        waterRuleQuery.setPageNo(pageId);
        waterRuleQuery.setPageSize(pageSize);
        modelMap.put("query",waterRuleQuery);
        BizResult bizResult = waterRuleBiz.list(waterRuleQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "fee/water/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "fee/water/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, WaterRuleEditForm waterRuleEditForm,
                       BindingResult result, Map<String,Object> model) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = waterRuleBiz.queryForEdit(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            waterRuleEditForm.initForm(((WaterRuleDO) bizResult.data.get("waterRuleDO")));
            return "fee/water/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/water/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = waterRuleBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "fee/water/detail.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/water/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, WaterRuleForm waterRuleForm,
                         BindingResult result) {
        return "fee/water/create.vm";
    }

    @RequestMapping(value = "fee/water/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid WaterRuleForm waterRuleForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            if(waterRuleForm.getVillageId()!=null){
                SysVarQuery sysVarQuery = new SysVarQuery();
                sysVarQuery.setVillageId(waterRuleForm.getVillageId());
                sysVarQuery.setVarType(SysVarTypeEnum.building.name());
                sysVarQuery.setPageSize(1000);
                List<SysVarDO> sysVarDOs = sysVarDAO.selectForView(sysVarQuery);
                modelMap.put("buildingTypeList",sysVarDOs);
            }
            return "fee/water/create.vm";
        }
        if(waterRuleForm.getMinValue() == null){

           if(waterRuleForm.getMaxValue() == null){
               result.rejectValue("maxValue","notNull","不能为空");
               return "fee/water/create.vm";
           }
            waterRuleForm.setMinValue(0);
        }
        if(waterRuleForm.getMaxValue()!= null && waterRuleForm.getMinValue()!=null && waterRuleForm.getMinValue() !=0){
             if(waterRuleForm.getMaxValue() < waterRuleForm.getMinValue()){
                 int water = waterRuleForm.getMaxValue();
                 waterRuleForm.setMaxValue(waterRuleForm.getMinValue());
                 waterRuleForm.setMinValue(water);
             }
        }
        WaterRuleDO waterRuleDO = waterRuleForm.toDO();
        BizResult bizResult = waterRuleBiz.create(waterRuleDO);
        if (bizResult.success) {
            return "redirect:/fee/water/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/water/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid WaterRuleEditForm waterRuleEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            if(waterRuleEditForm.getVillageId()!=null){
                SysVarQuery sysVarQuery = new SysVarQuery();
                sysVarQuery.setVillageId(waterRuleEditForm.getVillageId());
                sysVarQuery.setVarType(SysVarTypeEnum.building.name());
                sysVarQuery.setPageSize(1000);
                List<SysVarDO> sysVarDOs = sysVarDAO.selectForView(sysVarQuery);
                modelMap.put("buildingTypeList",sysVarDOs);
            }
            return "fee/water/edit.vm";
        }
        WaterRuleDO waterRuleDO = waterRuleEditForm.toDO();
        BizResult bizResult = waterRuleBiz.update(waterRuleDO);
        if (bizResult.success) {
            return "redirect:/fee/water/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "fee/water/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = waterRuleBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
